package com.ndp.fb.enums.publish;

import lombok.Getter;

/**
 * Created by jacky.cheng on 2015/8/12.
 */
@Getter
public enum OptimizationGoal {
    /**
     * Only available in read mode for campaigns created pre v2.4
     */
    NONE("NONE"),
    APP_INSTALLS("APP_INSTALLS"),
    BRAND_AWARENESS("BRAND_AWARENESS"),
    CLICKS("CLICKS"),
    ENGAGED_USERS("ENGAGED_USERS"),
    EXTERNAL("EXTERNAL"),
    EVENT_RESPONSES("EVENT_RESPONSES"),
    IMPRESSIONS("IMPRESSIONS"),
    LINK_CLICKS("LINK_CLICKS"),
    OFFER_CLAIMS("OFFER_CLAIMS"),
    OFFSITE_CONVERSIONS("OFFSITE_CONVERSIONS"),
    PAGE_ENGAGEMENT("PAGE_ENGAGEMENT"),
    PAGE_LIKES("PAGE_LIKES"),
    POST_ENGAGEMENT("POST_ENGAGEMENT"),
    REACH("REACH"),
    SOCIAL_IMPRESSIONS("SOCIAL_IMPRESSIONS"),
    VIDEO_VIEWS("VIDEO_VIEWS"),;
    private String code;

    OptimizationGoal(String code) {
        this.code = code;
    }

    /**
     * 解析字符串,不区分大小写，已经trim。
     *
     * @param code
     * @return
     */
    public static OptimizationGoal parse(String code) {
        for (OptimizationGoal c : OptimizationGoal.values()) {
            if (c.code.equalsIgnoreCase(code.trim())) {
                return c;
            }
        }
        return null;
    }

    /**
     * 解析老版API（2.3），
     * 从bid_info中解析。
     *
     * @param code 必须是一下几种的一个。
     *             <ul>
     *             <li>CLICKS</li>
     *             <li>IMPRESSIONS</li>
     *             <li>ACTIONS</li>
     *             <li>REACH</li>
     *             </ul>
     * @return
     */
    public static OptimizationGoal parseOldVersion(String code) {
        if ("CLICKS".equalsIgnoreCase(code.trim())) {
            return LINK_CLICKS;
        } else if ("IMPRESSIONS".equalsIgnoreCase(code.trim())) {
            return IMPRESSIONS;
        } else if ("ACTIONS".equalsIgnoreCase(code.trim())) {
            return APP_INSTALLS;
        } else if ("REACH".equalsIgnoreCase(code.trim())) {
            return REACH;
        } else if ("SOCIAL".equalsIgnoreCase(code.trim())) {
            return null; //todo social
        }
        return null;
    }

    public static String toOldVersion(String code) {
        if (LINK_CLICKS.getCode().equals(code)) {
            return "CLICKS";
        } else if (IMPRESSIONS.getCode().equals(code)) {
            return "IMPRESSIONS";
        } else if (APP_INSTALLS.getCode().equals(code)) {
            return "ACTIONS";
        } else if (REACH.getCode().equals(code)) {
            return "REACH";
        }
        return null;
    }
}
